local Log = require("common.Log")
local Lang = require("common.Lang")
local Json = require("common.Json")
local Msg = require("core.Msg")
local Timer = require("core.Timer")
local Util = require("common.Util")
local ChatBan = require("chat.ChatBan")
local ChatRecord = require("chat.ChatRecord")
local Broadcast = require("broadcast.Broadcast")
local ChatLogic = require("chat.ChatLogic")
local CombatVideo = require("combat.CombatVideo")
local Define = require("platform.Define")
local HeroExcel = require("excel.hero")
local RoleLogic = require("role.RoleLogic")
local CharRecord = require("chat.ChatRecord")

-- 聊天频道
CHAT_TYPE_WORLD         = 1	    --世界
CHAT_TYPE_SYSTEM        = 2 	--系统
CHAT_TYPE_UNION	        = 3 	--工会
CHAT_TYPE_FRIEND        = 4 	--好友私聊
CHAT_TYPE_MIDDLE        = 5 	--跨服

CHAT_TYPE_CNT = 5           --聊天频道数

-- isJson值
CHAT_NORMAL         = 0         -- 普通聊天 
CHAT_UNION_FIGHT    = 1         -- 公会战
CHAT_UNION_ECTYPE   = 2         -- 公会副本
CHAT_UNION_RED_BAG  = 3         -- 公会红包
CHAT_UNION_ZHAOMU   = 4         -- 公会招募
CHAT_HERO_SHARE     = 5         -- 英雄聊天分享
CHAT_FIGHT_SHARE    = 6         -- 战斗记录聊天分享
CHAT_ITEM_SHARE     = 7         -- 道具聊天分享
                      
CHAT_SHARE_TYPE     = 1         -- 战报分享
CHAT_LV_NEED_WORLD        = 20	    -- 开放聊天等级
CHAT_LV_NEED_MIDDLE        = 50	    -- 开放聊天等级
CHAT_LV_NEED_FRIEND        = 30	    -- 开放聊天等级
CHAT_CD             = 10000		-- 聊天cd时间
CHAT_MSG_LEN        = 90

function chatReport(human, content)
	s2aParam = {}

    s2aParam.role_name = human.db.name
    s2aParam.account_name = human.db.account
    s2aParam.content = content or ""
    s2aParam.ip = human.db.ip
    
	_G.thread_http.send(Define.CHAT_REPORT,Json.Encode(s2aParam))
    Log.write(Log.LOGID_DEBUG, "chatReportSuccrss"..Define.CHAT_REPORT) 
end

function CG_CHAT_REPORT(human, msg)
    chatReport(human, msg.content)
end

function CG_CHAT(human, msg)
    local strLen = string.len(msg.msg)
    -- 普通聊天限制字数
    if CHAT_MSG_LEN < strLen and msg.isJson == CHAT_NORMAL then
        return Broadcast.sendErr(human, Lang.CHAT_MSG_LEN_ERR)	
    end
    ChatLogic.chat(human, msg, msg.isJson)
end

function onLogin(human)
    ChatRecord.initHumanChatRead(human)
	ChatBan.sendBanList(human)
end

function CG_CHAT_BAN(human, msg)
	ChatBan.setBan(human, msg.uuid, msg.op)
end

function CG_CHAT_PLAYER_INFO(human, msg)
	local msgRet = Msg.gc.GC_CHAT_PLAYER_INFO
	if not RoleLogic.makePlayInfo(msgRet.data, msg.uuid) then
		return
	end
	Msg.send(msgRet, human.fd)
end

function CG_CHAT_HERO_SHARE(human,msg)
	local bagIndex = msg.bagIndex
	local heroGrid = human.db.heroBag[bagIndex]
	if not heroGrid then
		return
	end
	
	local heroConfig = HeroExcel.hero[heroGrid.id]
    if 	heroConfig == nil then
        return 		
	end
    msg.videoUuid = ""
	ChatLogic.chat(human, msg,CHAT_HERO_SHARE)
end

--战斗界面 战斗记录分享
function CG_CHAT_COMBAT_SHARE(human, msg)
	if msg.msgType ~= CHAT_TYPE_WORLD and msg.msgType ~= CHAT_TYPE_UNION and msg.msgType ~= CHAT_TYPE_MIDDLE then
		return
	end
	
	-- 如果是分享在世界上检测是否在聊天CD 中
	if msg.msgType == CHAT_TYPE_WORLD and human.worldChatTime and human.worldChatTime + CHAT_CD > Timer.now then
		local cdLeftSec = math.ceil((CHAT_CD - (Timer.now - human.worldChatTime))/1000)
		Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
		return
	end
    
	if msg.msgType == CHAT_TYPE_UNION and human.db.unionUuid == nil then
		return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
	end
    local content = {}
    content.msg = msg.msg
    content.msgType = msg.msgType
    if msg.shareType ~= CHAT_SHARE_TYPE then
        local combatInfo = Util.copyTable(human.combat)	
	    if combatInfo == nil then
	    	return
	    end	 
    

	    local videoUuid = human.combat.videoUuid
	    if not videoUuid then
	    	videoUuid = CombatVideo.saveCombatVideo(human, true)		
	    end	

	    if not videoUuid then 
	    	return Broadcast.sendErr(human, Lang.SHARE_ERROR)
	    end
        
        content.videoUuid = videoUuid
    end
	local flag = ChatLogic.chat(human,content,CHAT_FIGHT_SHARE)
    if flag == true then
        Broadcast.sendErr(human, Lang.SHARE_SUCCESS)
    else
        Broadcast.sendErr(human, Lang.SHARE_ERROR)
    end
end

function CG_CHAT_COMPLAIN_PLAYER(human,msg) 
    ChatBan.jubaoChat(human,msg)
end

-- 获取聊天记录
function CG_CHAT_RECORD_QUERY(human,msg)
    CharRecord.getChatRecord(human,msg.msgType)
end

-- 获取私聊列表
function CG_CHAT_FRIEND_RECORD_QUERY(human)
    CharRecord.getChatFriendList(human)
end

--根据uuid获取私聊记录
function CG_CHAT_FRIEND_RECORD_BY_FRIEND(human,msg)
    CharRecord.getChatFriendRecord(human,msg.uuid)
end

-- 新增私聊
function CG_CHAT_FRIEND_RECORD_ADD(human,msg)
    CharRecord.addFriendChat(human,msg.uuid)
end

-- 删除玩家私聊数据
function CG_CHAT_FRIEND_RECORD_DEL(human,msg)
    CharRecord.delFriendChatRecord(human,msg.uuid)
end

